<template>
  <div class="box">
    <div class="scroll-view">
      <projectMsg :projectId="project_id" :hrefProject="true"></projectMsg>
      <div class="user-title-box">基础信息</div>
      <el-form inline :model="scsjtRuleForm" :rules="scsjtRules" ref="scsjtRuleForm" label-position="top">
        <el-form-item label="平面图文件 (.dwg,.pdf,.zip,.pptx,.ppt,.jpg,.png,.gif)" prop="plan_drawing">
          <div class="file-list-box">
            <el-upload accept=".dwg,.pdf,.zip,.pptx,.ppt,.jpg,.png,.gif" multiple class="avatar-uploader" action="#"
              :show-file-list="false" :auto-upload="false" list-type="picture-card" :on-change="(file, fileList) => {
                scsjtFileChange(file, fileList, 'plan_drawing');
              }
                ">
              <i class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
            <div class="file-item-box" v-for="(item, index) in scsjtRuleForm.plan_drawing" :key="index">
              <div class="el-icon-delete-box" @click="scsjtFileRemove(index, 'plan_drawing')">
                <i class="el-icon-error"></i>
              </div>
              <el-tooltip class="item" effect="dark" :content="item.name || item.file_name" placement="top">
                <img class="img" :src="$fileImgFunc(item)" />
              </el-tooltip>
            </div>
          </div>
        </el-form-item>
        <el-form-item label="效果图文件 (.zip,.ppt,.ppsx,.pptx,.psd,.pdf,.jpg,.png,.gif)" prop="effect_drawing">
          <div class="file-list-box">
            <el-upload accept=".zip,.ppt,.ppsx,.pptx,.psd,.pdf,.jpg,.png,.gif" multiple class="avatar-uploader"
              action="#" :show-file-list="false" :auto-upload="false" list-type="picture-card" :on-change="(file, fileList) => {
                scsjtFileChange(file, fileList, 'effect_drawing');
              }
                ">
              <i class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
            <div class="file-item-box" v-for="(item, index) in scsjtRuleForm.effect_drawing" :key="index">
              <div class="el-icon-delete-box" @click="scsjtFileRemove(index, 'effect_drawing')">
                <i class="el-icon-error"></i>
              </div>
              <el-tooltip class="item" effect="dark" :content="item.name || item.file_name" placement="top">
                <img class="img" :src="$fileImgFunc(item)" />
              </el-tooltip>
            </div>
          </div>
        </el-form-item>
        <el-form-item label="施工图文件 (.zip,.ppt,.ppsx,.pptx,.psd,.pdf,.dwg,.jpg,.png,.gif)" prop="construction_drawing">
          <div class="file-list-box">
            <el-upload accept=".zip,.ppt,.ppsx,.pptx,.psd,.pdf,.dwg,.jpg,.png,.gif" multiple class="avatar-uploader"
              action="#" :show-file-list="false" :auto-upload="false" list-type="picture-card" :on-change="(file, fileList) => {
                scsjtFileChange(file, fileList, 'construction_drawing');
              }
                ">
              <i class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
            <div class="file-item-box" v-for="(item, index) in scsjtRuleForm.construction_drawing" :key="index">
              <div class="el-icon-delete-box" @click="scsjtFileRemove(index, 'construction_drawing')">
                <i class="el-icon-error"></i>
              </div>
              <el-tooltip class="item" effect="dark" :content="item.name || item.file_name" placement="top">
                <img class="img" :src="$fileImgFunc(item)" />
              </el-tooltip>
            </div>
          </div>
        </el-form-item>
        <el-form-item label="其他文件">
          <div class="file-list-box">
            <el-upload multiple class="avatar-uploader" action="#" :show-file-list="false" :auto-upload="false"
              list-type="picture-card" :on-change="(file, fileList) => {
                scsjtFileChange(file, fileList, 'other_file');
              }
                ">
              <i class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
            <div class="file-item-box" v-for="(item, index) in scsjtRuleForm.other_file" :key="index">
              <div class="el-icon-delete-box" @click="scsjtFileRemove(index, 'other_file')">
                <i class="el-icon-error"></i>
              </div>
              <el-tooltip class="item" effect="dark" :content="item.name || item.file_name" placement="top">
                <img class="img" :src="$fileImgFunc(item)" />
              </el-tooltip>
            </div>
          </div>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input :rows="4" class="input-box" v-model="scsjtRuleForm.remark" type="textarea"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="scsjtSubmit('scsjtRuleForm')" :disabled="diaLoading"
          :loading="diaLoading">提交</el-button>
        <el-button @click="scsjtCancel('scsjtRuleForm')">关闭</el-button>
      </span>
    </div>
  </div>
</template>
<script>
import projectApi from "@/api/projectApi";
import designApi from "@/api/designApi";
import projectMsg from "@/components/ProjectMsg";
export default {
  name: "scsjtCom",
  components: {
    projectMsg,
  },
  data() {
    var file4 = (rule, value, callback) => {
      if (!value.length) {
        callback(new Error("请上传平面图文件"));
      } else {
        callback();
      }
    };
    var file5 = (rule, value, callback) => {
      if (!value.length) {
        callback(new Error("请上传效果图文件"));
      } else {
        callback();
      }
    };
    var file6 = (rule, value, callback) => {
      if (!value.length) {
        callback(new Error("请上传施工图文件"));
      } else {
        callback();
      }
    };
    return {
      scsjtRuleForm: {
        remark: "",
        plan_drawing: [],
        effect_drawing: [],
        construction_drawing: [],
        other_file: [],
      },
      scsjtRules: {
        plan_drawing: {
          required: true,
          validator: file4,
          trigger: "change",
        },
        effect_drawing: {
          required: true,
          validator: file5,
          trigger: "change",
        },
        construction_drawing: {
          required: true,
          validator: file6,
          trigger: "change",
        },
      },
      diaLoading: false,
      project_id: "",
      delIdArr: [],//被删除文件id
      statusVal: '',//项目状态
      isChange: false,//判断是否有改动
      imgLength: 0,//文件数  （修改时判断）
    };
  },
  mounted() {
    if (this.$route.query.id) {
      this.project_id = this.$route.query.id;
      this.designProjectDetails();
    }
  },
  methods: {
    // 设计详情A
    designProjectDetails() {
      if (!this.$route.query.id) return;
      designApi.designProjectDetails({
        project_id: this.$route.query.id
      }).then(res => {
        console.log(res, "设计图详情")
        if (res.code == 200) {
          let project_file = res.data.project_file;
          this.statusVal = res.data.status;
          this.imgLength =
            project_file.plan_drawing.length +
            project_file.effect_drawing.length +
            project_file.construction_drawing.length +
            project_file.other_file.length;
          this.scsjtRuleForm = {
            id: res.data.id,
            remark: "",
            plan_drawing: project_file.plan_drawing,
            effect_drawing: project_file.effect_drawing,
            construction_drawing: project_file.construction_drawing,
            other_file: project_file.other_file,
          }
        }
      })
    },
    scsjtCancel(formName) {
      this.$goBackTab(this);
    },
    scsjtSubmit(formName) {
      let scsjtRuleForm = this.scsjtRuleForm;
      console.log(this.delIdArr, scsjtRuleForm)
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let formdata = new FormData();
          formdata.append(
            "data",
            JSON.stringify({
              remark: scsjtRuleForm.remark,
              project_id: this.project_id,
              delete_arr: this.delIdArr
            })
          );
          scsjtRuleForm.plan_drawing.forEach((item) => {
            if (item.raw) {
              formdata.append("plan_drawing[]", item.raw);
            }
          });
          scsjtRuleForm.effect_drawing.forEach((item) => {
            if (item.raw) {
              formdata.append("effect_drawing[]", item.raw);
            }
          });
          scsjtRuleForm.construction_drawing.forEach((item) => {
            if (item.raw) {
              formdata.append("construction_drawing[]", item.raw);
            }
          });
          scsjtRuleForm.other_file.forEach((item) => {
            if (item.raw) {
              formdata.append("other_file[]", item.raw);
            }
          });
          if (this.statusVal == 1015 || this.statusVal == 1060) {
            // 添加
            this.uploadDesignDrawings(formdata);
          } else {

            let imgLength2 =
              scsjtRuleForm.plan_drawing.length +
              scsjtRuleForm.effect_drawing.length +
              scsjtRuleForm.construction_drawing.length +
              scsjtRuleForm.other_file.length;

            // 更新
            if (this.imgLength != imgLength2 || this.isChange) {
              this.updDesignDrawings(formdata);
            } else {
              this.$message({
                type: 'warning',
                message: '没有改动，无需提交'
              })
            }
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 上传设计图
    uploadDesignDrawings(formdata) {
      this.diaLoading = true;
      projectApi.uploadDesignDrawings(formdata).then((res) => {
        console.log(res, "上传设计图");
        this.diaLoading = false;
        if (res.code == 200) {
          this.scsjtCancel();
        }
        this.$message({
          type: res.code == 200 ? "success" : "warning",
          message: res.code == 200 ? "处理成功" : res.message,
        });
      })
        .catch((err) => {
          this.diaLoading = false;
          this.$message({
            type: "warning",
            message: err.message,
          });
        });
    },
    // 更新设计图
    updDesignDrawings(formdata) {
      this.diaLoading = true;
      projectApi.updDesignDrawings(formdata).then(res => {
        console.log(res, "更新设计图")
        this.diaLoading = false;
        if (res.code == 200) {
          this.scsjtCancel();
        }
        this.$message({
          type: res.code == 200 ? "success" : "warning",
          message: res.code == 200 ? "处理成功" : res.message,
        });
      }).catch((err) => {
        this.diaLoading = false;
        this.$message({
          type: "warning",
          message: err.message,
        });
      });
    },
    scsjtFileChange(file, fileList, type) {
      console.log(fileList, type);
      let scsjtRuleForm = this.scsjtRuleForm;
      this.isChange = true;
      switch (type) {
        // 平面图文件
        case "plan_drawing":
          scsjtRuleForm.plan_drawing.push(file);
          break;
        // 效果图文件
        case "effect_drawing":
          scsjtRuleForm.effect_drawing.push(file);
          break;
        // 施工图文件
        case "construction_drawing":
          scsjtRuleForm.construction_drawing.push(file);
          break;
        // 其他文件
        case "other_file":
          scsjtRuleForm.other_file.push(file);
          break;

        default:
      }
      this.scsjtRuleForm = scsjtRuleForm;
    },
    scsjtFileRemove(index, type) {
      console.log(index, type);
      let scsjtRuleForm = this.scsjtRuleForm;
      let delIdArr = this.delIdArr;
      this.isChange = true;
      switch (type) {
        // 其他文件
        case "other_file":
          if (scsjtRuleForm.other_file[index].file_id) {
            scsjtRuleForm.other_file[index].type = 'other_file';
            scsjtRuleForm.other_file[index].delete = true;
            delIdArr.push(scsjtRuleForm.other_file[index]);
          }
          scsjtRuleForm.other_file.splice(index, 1);
          break;
        // 平面图文件
        case "plan_drawing":
          if (scsjtRuleForm.plan_drawing[index].file_id) {
            scsjtRuleForm.plan_drawing[index].type = 'plan_drawing';
            scsjtRuleForm.plan_drawing[index].delete = true;
            delIdArr.push(scsjtRuleForm.plan_drawing[index]);
          }
          scsjtRuleForm.plan_drawing.splice(index, 1);
          break;
        // 效果图文件
        case "effect_drawing":
          if (scsjtRuleForm.effect_drawing[index].file_id) {
            scsjtRuleForm.effect_drawing[index].type = 'effect_drawing';
            scsjtRuleForm.effect_drawing[index].delete = true;
            delIdArr.push(scsjtRuleForm.effect_drawing[index]);
          }
          scsjtRuleForm.effect_drawing.splice(index, 1);
          break;
        // 施工图文件
        case "construction_drawing":
          if (scsjtRuleForm.construction_drawing[index].file_id) {
            scsjtRuleForm.construction_drawing[index].type = 'construction_drawing';
            scsjtRuleForm.construction_drawing[index].delete = true;
            delIdArr.push(scsjtRuleForm.construction_drawing[index]);
          }
          scsjtRuleForm.construction_drawing.splice(index, 1);
          break;
        default:
      }
      this.delIdArr = delIdArr;
      this.scsjtRuleForm = scsjtRuleForm;
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/styles/projectOparet.scss";
@import "@/styles/addPage.scss";
</style>